package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.generateRFC;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.CMSThread;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.HistoryMaker;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.generateRFC.subs.DataInputValueTester;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.GenerateRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.generateRFCSubs.DatainputPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.generateRFCSubs.GenRFCFunctionPanel;

public class CommittRFCThread extends CMSThread {

	private DatainputPanel datainputPanel;
	private DataInputValueTester dataInputValueTester;
	private JLabel progress;
	private GenerateRFCPanel generateRFCPanel;
	private GenRFCFunctionPanel functionPanel;

	public CommittRFCThread(GenerateRFCPanel generateRFCPanel,
			GenRFCFunctionPanel functionPanel, JLabel progress) {

		this.functionPanel = functionPanel;
		this.generateRFCPanel = generateRFCPanel;
		this.progress = progress;
		this.datainputPanel = generateRFCPanel.getDatainputPanel();
		this.dataInputValueTester = new DataInputValueTester(datainputPanel);
	}

	@Override
	public void run() {

		this.changeStateofComponents(datainputPanel.getallUserComponents());
		this.functionPanel.getLb_progressbar().setVisible(true);

		if (this.dataInputValueTester.checkallValuesSmallcheck()) {

			boolean issaved = this.saveRFCinDB(datainputPanel, progress);

			this.functionPanel.getLb_progressbar().setVisible(true);
			if (issaved) {

				DatabaseConnection connection = new DatabaseConnection();
				connection.connect();
				String query = "UPDATE CMS_RFC SET " + RFC.TC_Status
						+ " = 'open' WHERE " + RFC.TC_ID + " = '"
						+ datainputPanel.getLb_IDValue().getText() + "'";
				connection.insertquery(query);
				connection.disconnect();

				generateRFCPanel.setVisible(false);
				generateRFCPanel.getDatainputPanel().removeAllPanles();
				generateRFCPanel.setVisible(true);

				this.generateRFCPanel
						.getMainframe()
						.getTrayLoader()
						.createSysTrayMessage("Simple CMS",
								"Eine neue Nachricht wurde dem ChangeOwner zugewiesen");

				this.functionPanel.getBu_newRFC().setEnabled(true);
				this.functionPanel.getBu_editButton().setEnabled(true);
				this.functionPanel.getBu_cancel().setEnabled(false);
				this.functionPanel.getBu_save().setEnabled(false);
				this.functionPanel.getBu_sendRFC().setEnabled(false);
				this.functionPanel.getBu_deleteRFC().setEnabled(false);
				this.functionPanel.getLb_progressbar().setVisible(false);
//***************** HISTORY
				new HistoryMaker().addHistoryInDBTable(
						datainputPanel.getLb_generaterValue().getText(),
						datainputPanel.getLb_IDValue().getText(), "RFC erstellt");
//***********************
			} else {
				this.changeStateofComponents(datainputPanel
						.getallUserComponents());
				JOptionPane.showMessageDialog(datainputPanel,
						"Daten konnten nicht abgespeichert werden");
				progress.setVisible(false);

				generateRFCPanel.revalidate();

			}
		} else {

			
			this.changeStateofComponents(datainputPanel.getallUserComponents());
			JOptionPane.showMessageDialog(datainputPanel,
					"Mindestens ein Wert ist fehlerhaft");

			progress.setVisible(false);

			generateRFCPanel.revalidate();
		}

	}
}
